package com.ruoyi.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.domain.SpaceDirectory;
import com.ruoyi.domain.SpaceDocument;
import com.ruoyi.domain.SpaceRecord;
import com.ruoyi.dto.SpaceDocumentDetail;
import com.ruoyi.mapper.SpaceDocumentMapper;
import com.ruoyi.platform.service.impl.BaseServiceImpl;
import com.ruoyi.platform.utils.SecurityUtils;
import com.ruoyi.platform.utils.StringUtils;
import com.ruoyi.service.ISpaceDirectoryService;
import com.ruoyi.service.ISpaceDocumentService;
import com.ruoyi.service.ISpaceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 智联空间文档Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-19
 */
@Service
public class SpaceDocumentServiceImpl extends BaseServiceImpl<SpaceDocumentMapper, SpaceDocument> implements ISpaceDocumentService {

    @Autowired
    private SpaceDocumentMapper spaceDocumentMapper;

    @Autowired
    private ISpaceDirectoryService spaceDirectoryService;

    @Autowired
    private ISpaceRecordService spaceRecordService;

    /**
     * 查询智联空间文档
     *
     * @param id 智联空间文档主键
     * @return 智联空间文档
     */
    @Override
    public SpaceDocument selectSpaceDocumentById(Long id) {
        return spaceDocumentMapper.selectSpaceDocumentById(id);
    }

    /**
     * 查询智联空间文档列表
     *
     * @param spaceDocument 智联空间文档
     * @return 智联空间文档
     */
    @Override
    public List<SpaceDocument> selectSpaceDocumentList(SpaceDocument spaceDocument) {
        return spaceDocumentMapper.selectSpaceDocumentList(spaceDocument);
    }

    /**
     * 新增智联空间文档
     *
     * @param spaceDocument 智联空间文档
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSpaceDocument(List<SpaceDocument> spaceDocument) {
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        int flag = 0;

        SpaceDirectory spaceDirectory = new SpaceDirectory();
        spaceDirectory.setParentId(spaceDocument.get(0).getDirectoryId());
        if (StringUtils.isNotEmpty(spaceDocument.get(0).getDirectoryName())) {
            SpaceDirectory spaceDirectoryServiceOne = spaceDirectoryService.getOne(new LambdaQueryWrapper<SpaceDirectory>().eq(SpaceDirectory::getName, spaceDocument.get(0).getDirectoryName()));
            spaceDirectory.setParentId(spaceDirectoryServiceOne.getId());
        }
        for (SpaceDocument document : spaceDocument) {
            SpaceDocument spaceDocument1 = new SpaceDocument();
            spaceDocument1.setDescription(document.getDescription());
            spaceDocument1.setType(document.getType());
            spaceDocument1.setSuffix(document.getSuffix());

            spaceDocument1.setFileId(document.getFileId());
            spaceDocument1.setName(document.getName());
            Assert.isTrue(baseMapper.selectDocumentByName(document.getName()).size() == 0, "失败，文档名称已存在");
            spaceDocument1.setAuthor(nickName);
            flag = baseMapper.insert(spaceDocument1);

            spaceDirectory.setType(document.getDirectoryType());
            spaceDirectory.setDocumentId(spaceDocument1.getId());
            spaceDirectory.setName(spaceDocument1.getName());
            spaceDirectoryService.insertSpaceDirectory(spaceDirectory);

            SpaceRecord spaceRecord = new SpaceRecord();
            spaceRecord.setDocumentId(spaceDocument1.getId());
            spaceRecord.setType(1L);
            spaceRecordService.insertSpaceRecord(spaceRecord);
        }


//        spaceDocument.setAuthor(nickName);
//        Assert.isTrue(baseMapper.selectDocumentByName(spaceDocument.getName()).size() == 0, "失败，文档名称已存在");
//        flag = baseMapper.insert(spaceDocument);


//        SpaceDirectory spaceDirectory = new SpaceDirectory();
//        spaceDirectory.setParentId(spaceDocument.getDirectoryId());
//        if (StringUtils.isNotEmpty(spaceDocument.getDirectoryName())) {
//            SpaceDirectory spaceDirectoryServiceOne = spaceDirectoryService.getOne(new LambdaQueryWrapper<SpaceDirectory>().eq(SpaceDirectory::getName, spaceDocument.getDirectoryName()));
//            spaceDirectory.setParentId(spaceDirectoryServiceOne.getId());
//        }
//        spaceDirectory.setType(spaceDocument.getDirectoryType());
//        spaceDirectory.setDocumentId(spaceDocument.getId());
//        spaceDirectory.setName(spaceDocument.getName());
//        spaceDirectoryService.insertSpaceDirectory(spaceDirectory);

//        SpaceRecord spaceRecord = new SpaceRecord();
//        spaceRecord.setDocumentId(spaceDocument.getId());
//        spaceRecord.setType(1L);
//        spaceRecordService.insertSpaceRecord(spaceRecord);
        return flag;
    }

    /**
     * 修改智联空间文档
     *
     * @param spaceDocument 智联空间文档
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSpaceDocument(SpaceDocument spaceDocument) {
        SpaceDocument oldDocument = baseMapper.selectById(spaceDocument.getId());
        SpaceDirectory directory = spaceDirectoryService.getOne(new LambdaQueryWrapper<SpaceDirectory>()
                .eq(SpaceDirectory::getDocumentId, oldDocument.getId()));
        SpaceDirectory spaceDirectory = new SpaceDirectory();
        spaceDirectory.setId(directory.getId());
        spaceDirectory.setParentId(spaceDocument.getDirectoryId());
        spaceDirectory.setName(directory.getName());
        spaceDirectoryService.updateById(spaceDirectory);
        return baseMapper.updateById(spaceDocument);
    }

    /**
     * 批量删除智联空间文档
     *
     * @param ids 需要删除的智联空间文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpaceDocumentByIds(Long[] ids) {
        return spaceDocumentMapper.deleteSpaceDocumentByIds(ids);
    }

    /**
     * 删除智联空间文档信息
     *
     * @param id 智联空间文档主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpaceDocumentById(Long id) {
        return spaceDocumentMapper.deleteSpaceDocumentById(id);
    }

    @Override
    public List<Map<String, Object>> getDocTypeStatInfo() {
        return spaceDocumentMapper.selectDocTypeStatInfo();
    }

    @Override
    public List<SpaceDocumentDetail> selectImagesUnderDirectory(Long docId) {
        return spaceDocumentMapper.selectImagesUnderDirectory(docId);
    }

}
